import 'package:flutter/material.dart';
import 'package:qunaimedical/components/edit_text_field.dart';
import 'package:qunaimedical/components/primary_button.dart';
import 'package:qunaimedical/components/primary_label.dart';
import 'package:qunaimedical/utils/common_util.dart';
import 'package:qunaimedical/utils/toast_util.dart';

// 2022.12.6 mod lyy
import 'package:qunaimedical/utils/sp_util.dart';
import 'package:dio/dio.dart';
import 'package:qunaimedical/utils/dialog_util.dart';
import 'package:qunaimedical/values/urls.dart';
import 'package:qunaimedical/network/http.dart';
import 'package:qunaimedical/utils/log_util.dart';


/// 意见反馈
class FeedbackPage extends StatefulWidget {
  const FeedbackPage({Key? key}) : super(key: key);

  @override
  _FeedbackPageState createState() => _FeedbackPageState();
}

class _FeedbackPageState extends State<FeedbackPage> {
  // 2022.12.6 mod lyy
  final String _tag = '_FeedbackPageState';

  TextEditingController _controller = TextEditingController();

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('后续反馈'),
      ),
      body: ListView(
        children: [
          PrimaryLabel('后续反馈'),
          EditTextField(
            maxLines: 5,
            maxLength: 400,
            hintText: '请描述您所遇到的问题',
            controller: _controller,
          ),
          PrimaryButton('提交', onPressed: _post),
        ],
      ),
    );
  }

  void _post() async {
    CommonUtil.hideKeyboard(context);
    String content = _controller.text.trim();
    if (content.isEmpty) {
      ToastUtil.show('内容不能为空');
      return;
    }
    else{
      // 2022.12.6 mod lyy
      _postFeedBack();
    }
  }

  void _postFeedBack() async{
    Log.d(_tag, "_postFeedBack >> form data: ${ SPUtil.getUserName()}");

    FormData formData = FormData.fromMap({
      "autoTest":  "autoLogin",
      "userName":  SPUtil.getUserName(),
      "password":  SPUtil.getPassword(),

      "questionType": "appOther",
      "reportctx": _controller.text.trim(),
    });

    try {
      DialogUtil.showWaitDialog(context);
      var response =
      await XHttp.instance.post(Urls.ME_FEEDBACK, data: formData);
      Log.d(_tag, "_postFeedBack >> req send");
      if (mounted) {
        Navigator.pop(context);
        if (response.statusCode == 200 ) {
            Log.d(_tag, "_postFeedBack >> resp ok");
            ToastUtil.show('提交成功');
        }
      }
    } on DioError catch (e) {
      if (mounted) {
        Navigator.pop(context);
        CommonUtil.catchDioError(e);
      }
    }
  }
}
